package com.wentao.mall.service.impl;
import com.wentao.mall.exception.MallException;
import com.wentao.mall.exception.MallExceptionEnum;
import com.wentao.mall.mapper.CartMapper;
import com.wentao.mall.mapper.ProductMapper;
import com.wentao.mall.model.po.Cart;
import com.wentao.mall.model.po.Product;
import com.wentao.mall.model.po.User;
import com.wentao.mall.model.vo.CartVo;
import com.wentao.mall.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<CartVo> cartList(Integer userId) {
        List<CartVo> cartVoList=cartMapper.selectCartVoByUserId(userId);
        return cartVoList;
    }

    @Override
    public void addCart(Integer productId, Integer count, User currentUser) {
        //先判断商品id是否存在，且count值小于stock库存值
        Product product = productMapper.selectByPrimaryKey(productId);
        //判断购物车中商品状态，下架就添加失败
        if (product!=null){
            //判断商品状态，在售则继续操作
            if (product.getStatus()==0){
                throw new MallException(MallExceptionEnum.PRODUCT_NOT_SELL);
            }
            //如果有该商品，且用户已经加入购物车，则只加购物车记录中的数量即可
            Cart cart=cartMapper.selectByProductIdAndUserId(productId, currentUser.getId());
            if (cart!=null){
                //如果添加的数量加购物车中该商品已有数量 大于 stock库存，则添加失败，库存不足
                if(cart.getQuantity()+count>product.getStock()){
                    throw new MallException(MallExceptionEnum.STOCK_SHORTAGE);
                }
                int result= cartMapper.updateByIDAndCount(cart.getId(), count);
                if (result==0){
                    throw new MallException(MallExceptionEnum.UPDATE_FAINED);
                }
                return;
            }
            //如果没有商品，就直接加一条购物车数据
            //库存不足，直接抛出异常
            if (product.getStock()<count){
                throw new MallException(MallExceptionEnum.STOCK_SHORTAGE);
            }
            Cart newCart = new Cart();
            newCart.setProductId(productId);
            newCart.setQuantity(count);
            newCart.setUserId(currentUser.getId());
            int result = cartMapper.insertSelective(newCart);
            if (result==0){
                throw new MallException(MallExceptionEnum.USER_INSERT_FAILED);
            }
        }else{
            throw new MallException(MallExceptionEnum.PRODUCT_NOT_EXIST);
        }
    }

    @Override
    public void updateCart(Integer productId, Integer count, User currentUser) {
        //找出商品
        Product product = productMapper.selectByPrimaryKey(productId);
        //如果商品已下架就不允许更新购物车
        if (product.getStatus()==0){
            throw new MallException(MallExceptionEnum.PRODUCT_NOT_SELL);
        }
        //找出用户购物车中该商品库存
        //如果购物车数量大于stock，则抛出异常
        if(count>product.getStock()){
            throw new MallException(MallExceptionEnum.STOCK_SHORTAGE);
        }
        int result= cartMapper.updateToCount(productId,currentUser.getId(), count);
        if (result==0){
            throw new MallException(MallExceptionEnum.UPDATE_FAINED);
        }
    }

    @Override
    public void deleteCart(Integer productId,User user) {
        int count = cartMapper.deleteByUserIdAndProductId(productId,user.getId());
        if (count==0) {
            throw new MallException(MallExceptionEnum.DELETE_FAILED);
        }
    }

    @Override
    public void selectedCart(Integer productId, Integer selected, User user) {
        int count = cartMapper.updateBySelected(productId, selected, user.getId());
        if (count==0) {
            throw new MallException(MallExceptionEnum.UPDATE_FAINED);
        }
    }

    @Override
    public void selectedAll(Integer selected,User user) {
        cartMapper.selectAll(selected, user.getId());
    }
}
